package com.atguigu.tingshu.account.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Override
    public RechargeInfo getRechargeInfoByOrderNo(String orderNo) {
        // 根据订单号查询对象
        return this.getOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
    }

    @Override
    public Map<String, Object> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        RechargeInfo rechargeInfo = new RechargeInfo();
        Long userId = AuthContextHolder.getUserId();
        rechargeInfo.setUserId(userId);
        String orderNo = RandomStringUtils.random(15, true, true);
        rechargeInfo.setOrderNo(orderNo);
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        int insert = rechargeInfoMapper.insert(rechargeInfo);
        log.info("保存充值记录:{}", insert == 0 ? "失败" : "成功");
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", rechargeInfo.getOrderNo());
        return map;
    }
}
